using System;
using CommandSystem;
using CustomCharacter.Ability;
using Exiled.API.Features;

namespace CustomCharacter.Commands.AbilityManagerCommands
{
    public class List : ICommand
    {
        public bool Execute(ArraySegment<string> arguments, ICommandSender sender, out string response)
        {
            if (!sender.CheckPermission(PlayerPermissions.ForceclassWithoutRestrictions))
            {
                response = "你没有权限使用这条命令！（需要权限：ForceclassWithoutRestrictions）";
                return false;
            }

            if (arguments.Count > 0)
            {
                if (!int.TryParse(arguments.At(0), out var id))
                {
                    response = "请输入一个有效的数字ID";
                    return false;
                }

                if (!Player.TryGet(id, out var player))
                {
                    response = "玩家不存在！";
                    return false;
                }

                if (!AbilityManager.Abilities.TryGetValue(player, out var ability))
                {
                    response = "该玩家没有技能";
                    return true;
                }

                response = "该玩家拥有以下技能：";
                foreach (var ab in ability)
                {
                    response += $"\n[{ab.Ability.AbilityId}] ({ab.Ability.Text})";
                }

                response += "\n-----";
                return true;
            }

            response = "所有已注册的技能：";
            foreach (var ab in AbilityManager.RegisteredAbilities)
            {
                response += $"\n[{ab.AbilityId}] ({ab.Text})";
            }

            return true;
        }

        public string Command { get; } = "list";
        public string[] Aliases { get; } = Array.Empty<string>();
        public string Description { get; } = "获取某个玩家或所有已注册技能";
    }
}